package com.zs.toolbox.web2j.common.web;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shuai.zhang  on 2018/9/22
 * @description
 */
public class WtAuthContext {
    /*
    * 获得用户ID
    * */
    public static Long getUserId(){
        try {
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return Long.parseLong((String)principal);
        }catch (Exception e) {
            return 0L;
        }
    }


    /*
    *
    * 获得权限列表
    * */
    public static List<String> getAuths() {
        Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        return authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
    }


}
